﻿using Microsoft.Owin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Json;

namespace TongTongMall.Oauth2._0.Owin.Context
{
    public static class OwinContextExtend
    {
        public static async Task JsonResult(this IOwinContext context, object valueObject)
        {
            byte[] body = new UTF8Encoding().GetBytes(valueObject.ToJsonString(true));
            context.Response.StatusCode = 400;
            context.Response.ContentType = "application/json;charset=UTF-8";
            context.Response.Headers.Set("Cache-Control", "no-cache");
            context.Response.Headers.Set("Pragma", "no-cache");
            context.Response.Headers.Set("Expires", "-1");
            context.Response.Headers.Set("Content-Length", body.Length.ToString(CultureInfo.InvariantCulture));
            await context.Response.WriteAsync(body, context.Request.CallCancelled);
        }
    }
}
